package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import Entity.WorkersDBEntity;
import android.util.Xml;


public class XmlWorkersParser {
	
	 private static final String ns = null;
	 public WorkersDBEntity parse(InputStream in) throws XmlPullParserException, IOException {   
		 try {
         XmlPullParser parser = Xml.newPullParser();
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);       
         parser.setInput(in , null);
         parser.nextTag();       
         return readWorkersXml(parser); 
	 } finally {
         in.close();
     }
 }
	 private WorkersDBEntity readWorkersXml(XmlPullParser parser) throws XmlPullParserException, IOException {
		 parser.require(XmlPullParser.START_TAG, ns, "data");
		 WorkersDBEntity wEntity = new WorkersDBEntity();
		 while (parser.next() != XmlPullParser.END_TAG) {
			 if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
			 String name = parser.getName();
			 if (name.equals("suppliers")) {
				 wEntity.setSuppliers(readSuppliers(parser)) ;}
	            else if (name.equals("cars")) {
	            	 wEntity.setCars(readCars(parser));
	        	}
	            else{
	            	 XmlTemplateParser.skip(parser);
	            }
		 }
		 return wEntity;
	 }
	 
	 private ArrayList<MyTreeSet> readSuppliers(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<MyTreeSet> list= new ArrayList<MyTreeSet>();
	        parser.require(XmlPullParser.START_TAG, ns, "suppliers");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("supplier")) {
	            	 list.add(readSupplier(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 
	 private OrderedTreeSet<String, String, String> readSupplier (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "sname");
	      key=  XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "sname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "scodep");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "scodep");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "scodef");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "scodef");
	        parser.next();
	        parser.next();
	        OrderedTreeSet <String, String, String> supplier = new  OrderedTreeSet<String, String, String>(key,value1,value2);
		 return  supplier;
	 }
	 
	 private ArrayList<MyTreeSet> readCars(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<MyTreeSet> list= new ArrayList<MyTreeSet>();
	        parser.require(XmlPullParser.START_TAG, ns, "cars");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("car")) {
	            	 list.add(readCar(parser)) ;}
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }
	  
	        return list;
	    }
	 
	 private OrderedTreeSet<String, String, String> readCar (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "cname");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "cname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "ccodeoil");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "ccodeoil");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "ccodezp");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "ccodezp");
	        parser.next();
	        parser.next();
	        OrderedTreeSet <String, String, String> supplier = new  OrderedTreeSet<String, String, String>(key,value1,value2);
		 return  supplier;
	 } 
	 
	
}
